package com.hfkh.keyun.controller.backend;

import com.hfkh.keyun.common.ApiResult;
import com.hfkh.keyun.entity.Unit;
import com.hfkh.keyun.service.UnitService;
import com.hfkh.keyun.util.PageResult;
import com.hfkh.keyun.vo.UnitVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/unit")
@Api(tags = "单元管理")
public class UnitController {
    @Resource
    private UnitService unitService;

    @GetMapping("/UnitList")
    @ApiOperation("根据专业id查询所有单元并分页")
    public ApiResult UnitList(@RequestParam(required = true,defaultValue = "")Integer majorId,@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "10") Integer size){
         PageResult<List<Unit>> listPageResult = unitService.UnitList(majorId, page, size);
        return ApiResult.success(listPageResult);
    }

    @PostMapping("/addUnit")
    @ApiOperation("新增单元")
    public ApiResult addUnit(@RequestBody UnitVo unitVo){
        Unit unit=new Unit();
        BeanUtils.copyProperties(unitVo,unit);
        unitService.save(unit);
        return ApiResult.ok("新增成功", "头像上传成功！");
    }

    @PostMapping("/updateUnit")
    @ApiOperation("修改单元")
    public ApiResult updateUnit(Integer id, UnitVo unitVo){
        Unit unit=new Unit();
        BeanUtils.copyProperties(unitVo,unit);
        unit.setId(id);
        unitService.updateById(unit);
        return ApiResult.ok("修改成功", "头像上传成功！");
    }

    @GetMapping("/deleteUnit")
    @ApiOperation("删除单元")
    public ApiResult deleteUnit(Integer id){
        unitService.removeById(id);
        return  ApiResult.ok("删除成功", "头像上传成功！");
    }


}
